package butines.core.gui.style;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

public class Box {

    public static enum Type {

        SOLID, RIDGED, ROUND;

    }

    private Type type = Type.SOLID;
    private int width;
    private int height;
    private int round = 16;
    private Color borderColorTop;
    private Color borderColorLeft;
    private Color borderColorBottom;
    private Color borderColorRight;
    private Color background1;
    private Color background2;
    private boolean cyclic;

    public Box() {
        
    }
    
    public Box copy() {
        Box other = new Box();
        other.type = type;
        other.width = width;
        other.height = height;
        other.round = round;
        other.borderColorTop = borderColorTop;
        other.borderColorLeft = borderColorLeft;
        other.borderColorBottom = borderColorBottom;
        other.borderColorRight = borderColorRight;
        other.background1 = background1;
        other.background2 = background2;
        other.cyclic = cyclic;
        return other;
    }
    
    public void draw(Graphics g, int x, int y) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.translate(x, y);

        if (background1 != null && background2 != null) {
            if (background1.equals(background2)) {
                g2d.setPaint(background1);
            } else {
                int hstr = cyclic ? height / 2 : height;
                g2d.setPaint(new GradientPaint(0, 0, background1, 0, hstr, background2, cyclic));
            }
        }

        if (type == Type.SOLID) {
            g2d.fillRect(0, 0, width, height);

            g2d.setColor(borderColorTop);
            g2d.drawLine(0, 0, width, 0);

            g2d.setColor(borderColorRight);
            g2d.drawLine(width, 0, width, height);

            g2d.setColor(borderColorBottom);
            g2d.drawLine(0, height, width, height);

            g2d.setColor(borderColorLeft);
            g2d.drawLine(0, 1, 0, height);
        } else if (type == Type.RIDGED) {
            g2d.fillRect(0, 0, width, height);

            g2d.setColor(borderColorLeft);
            g2d.drawLine(1, 1, width - 1, 1);
            g2d.drawLine(1, 1, 1, height - 1);

            g2d.setColor(borderColorRight);
            g2d.drawLine(width + 1, 0, width + 1, height + 1);
            g2d.drawLine(0, height + 1, width + 1, height + 1);

            g2d.setColor(borderColorTop);
            g2d.drawLine(0, 0, width, 0);
            g2d.drawLine(0, 1, 0, height);

            g2d.setColor(borderColorBottom);
            g2d.drawLine(width, 0, width, height);
            g2d.drawLine(0, height, width, height);
        } else if (type == Type.ROUND) {
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            g2d.fillRoundRect(0, 0, width, height, round, round);

            g2d.setStroke(new BasicStroke(2));
            g2d.setPaint(borderColorBottom);
            g2d.drawRoundRect(1, 1, width - 2, height - 2, round, round);

            g2d.setPaint(borderColorTop);
            g2d.setStroke(new BasicStroke(1));
            g2d.drawRoundRect(0, 0, width-1, height-1, round, round);
        }

        g2d.dispose();
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getRound() {
        return round;
    }

    public void setRound(int round) {
        this.round = round;
    }

    public Color getBorderColorTop() {
        return borderColorTop;
    }

    public void setBorderColorTop(Color borderColorTop) {
        this.borderColorTop = borderColorTop;
    }

    public Color getBorderColorLeft() {
        return borderColorLeft;
    }

    public void setBorderColorLeft(Color borderColorLeft) {
        this.borderColorLeft = borderColorLeft;
    }

    public Color getBorderColorBottom() {
        return borderColorBottom;
    }

    public void setBorderColorBottom(Color borderColorBottom) {
        this.borderColorBottom = borderColorBottom;
    }

    public Color getBorderColorRight() {
        return borderColorRight;
    }

    public void setBorderColorRight(Color borderColorRight) {
        this.borderColorRight = borderColorRight;
    }

    public Color getBackground1() {
        return background1;
    }

    public void setBackground1(Color background1) {
        this.background1 = background1;
    }

    public Color getBackground2() {
        return background2;
    }

    public void setBackground2(Color background2) {
        this.background2 = background2;
    }

    public boolean isCyclic() {
        return cyclic;
    }

    public void setCyclic(boolean cyclic) {
        this.cyclic = cyclic;
    }

    public void round(int radius, Color color, Color color2) {
        type = Type.ROUND;
        round = radius;
        borderColorTop = color;
        borderColorBottom = color2;
    }

    public void setSize(int w, int h) {
        width = w;
        height = h;
    }

    public void setBackground(Color color, Color color2) {
       this.background1 = color;
       this.background2 = color2;
    }

}
